package com.jingyanzi.member.controller;

import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.jingyanzi.member.api.SessionClientAPI;
import com.jingyanzi.member.domain.CustomerFamilyDO;
import com.jingyanzi.member.domain.LoginDO;
import com.jingyanzi.member.domain.result.ResultDO;
import com.jingyanzi.member.domain.result.ResultSupport;
import com.jingyanzi.member.query.CustomerFamilyQueryDO;
import com.jingyanzi.member.response.PageResponse;
import com.jingyanzi.member.service.ICustomerFamilyService;
//import com.jingyanzi.member.service.ICustomerFamilyService;
import com.jingyanzi.resallocation.domain.SingleRequest;
import com.jingyanzi.resallocation.result.SingleResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(value = "客户家庭成员管理 controller", tags = { "客户家庭成员操作接口" })
@RestController
@RequestMapping("/family")
public class CustomerFamilyController {

	public static final String SYS_ERROR = "{status:500, msg:\"系统错误\", version:1}";

	public static final String DES = "3DES";

	public static final String PLANINTEXT = "plaintext";

	@Autowired
	private ICustomerFamilyService customerFamilyService;
	
	@Autowired
	SessionClientAPI sessionClientAPI;

	private LoginDO getLoginDO(String token) {
		Base64.Encoder encoder = Base64.getEncoder();
		LoginDO loginDO = new LoginDO();
		try {
			String t = encoder.encodeToString(token.getBytes("utf-8"));
			loginDO = sessionClientAPI.getSession(t);
			if (loginDO == null) {
				loginDO = new LoginDO();
				loginDO.setId(1);
				loginDO.setMobile("13800000000");
				loginDO.setNickname("罗于红");
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return loginDO;
	}

	/**
	 * 查询联系人手机号是否有重复(可指定时间范围）
	 * 
	 * @param request
	 * @return
	 */
	@ApiOperation(value = "检查重复", notes = "通过手机号码检查重复")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "request", value = "客户家庭成员对象", required = true, dataType = "SingleRequest<CustomerFamilyDO>") })
	@RequestMapping(value = "/checkMobileNumber.json", method = RequestMethod.POST)
	public SingleResponse<CustomerFamilyDO> checkMobileNumber(@RequestBody SingleRequest<CustomerFamilyDO> request) {

		CustomerFamilyDO domain = request.getData();
		
		ResultDO result = customerFamilyService.checkMobileNumber(domain);
		SingleResponse<CustomerFamilyDO> response = new SingleResponse<CustomerFamilyDO>();
		if (result.isSuccess()) {
			response.setVersion(1);
			response.setStatus(200);
			response.setMsg("检查成功");
			response.setCount((Integer) result.getModel(ResultSupport.FIRST_MODEL_KEY));
		} else {
			response.setVersion(1);
			response.setStatus(500);
			response.setMsg("检查失败");
		}
		return response;
	}

	@ApiOperation(value = "创建客户家庭成员", notes = "创建客户家庭成员")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "request", value = "客户家庭成员对象", required = true, dataType = "SingleRequest<CustomerFamilyDO>") })
	@RequestMapping(value = "/add.json", method = RequestMethod.POST)
	public SingleResponse<CustomerFamilyDO> add(@RequestBody SingleRequest<CustomerFamilyDO> request) {

		CustomerFamilyDO domain = request.getData();
		LoginDO loginDO = getLoginDO(request.getToken());
		domain.setAuthorId(loginDO.getId());
		domain.setAuthor(loginDO.getNickname());

		ResultDO result = customerFamilyService.add(domain);
		SingleResponse<CustomerFamilyDO> response = new SingleResponse<CustomerFamilyDO>();
		if (result.isSuccess()) {
			response.setStatus(200);
			response.setVersion(1);
			response.setMsg("添加成功");
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("添加失败");
		}
		return response;
	}

	@ApiOperation(value = "修改客户家庭成员信息", notes = "修改客户家庭成员信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "request", value = "客户家庭成员对象", required = true, dataType = "SingleRequest<CustomerFamilyDO>") })
	@RequestMapping(value = "/edit.json", method = RequestMethod.POST)
	public SingleResponse<CustomerFamilyDO> edit(@RequestBody SingleRequest<CustomerFamilyDO> request) {

		CustomerFamilyDO domain = request.getData();
		LoginDO loginDO = getLoginDO(request.getToken());
		domain.setModifiId(loginDO.getId());
		domain.setModifi(loginDO.getNickname());

		ResultDO result = customerFamilyService.modifi(domain);
		SingleResponse<CustomerFamilyDO> response = new SingleResponse<CustomerFamilyDO>();
		if (result.isSuccess()) {
			response.setStatus(200);
			response.setVersion(1);
			response.setMsg("修改成功");
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("修改失败");
		}
		return response;
	}

	@ApiOperation(value = "获取客户家庭成员详情", notes = "获取客户家庭成员详情")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "request", value = "客户家庭成员对象(只传客户的ID)", required = true, dataType = "SingleRequest<CustomerFamilyDO>") })
	@RequestMapping(value = "/detail.json", method = RequestMethod.POST)
	public SingleResponse<CustomerFamilyDO> detail(@RequestBody SingleRequest<CustomerFamilyDO> request) {

		CustomerFamilyDO domain = request.getData();

		ResultDO result = customerFamilyService.get(domain.getId());
		SingleResponse<CustomerFamilyDO> response = new SingleResponse<CustomerFamilyDO>();
		if (result.isSuccess()) {
			response.setStatus(200);
			response.setVersion(1);
			response.setMsg("获取成功");
			response.setData((CustomerFamilyDO) result.getModel(ResultSupport.FIRST_MODEL_KEY));
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("获取失败");
		}
		return response;
	}

	@ApiOperation(value = "修改客户家庭成员扩展信息", notes = "修改用扩展信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "request", value = "客户家庭成员对象(只传客户的ID)", required = true, dataType = "SingleRequest<CustomerFamilyDO>") })
	@RequestMapping(value = "/delete.json", method = RequestMethod.POST)
	public SingleResponse<CustomerFamilyDO> delete(@RequestBody SingleRequest<CustomerFamilyDO> request) {

		CustomerFamilyDO domain = request.getData();

		ResultDO result = customerFamilyService.remove(domain.getId());
		SingleResponse<CustomerFamilyDO> response = new SingleResponse<CustomerFamilyDO>();
		if (result.isSuccess()) {
			response.setStatus(200);
			response.setVersion(1);
			response.setMsg("删除成功");
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("删除失败");
		}
		return response;
	}

	@ApiOperation(value = "查询客户家庭成员信息", notes = "查询客户家庭成员展信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "request", value = "客户查询对象", required = true, dataType = "SingleRequest<CustomerQueryDO>") })
	@RequestMapping(value = "/query.json", method = RequestMethod.POST)
	public PageResponse<CustomerFamilyDO> query(@RequestBody SingleRequest<CustomerFamilyQueryDO> request) {

		CustomerFamilyQueryDO query = request.getData();
		if (query.getPageRows() > 0) {
			query.setPage(true);
		}
		ResultDO result = customerFamilyService.query(query);

		PageResponse<CustomerFamilyDO> response = new PageResponse<CustomerFamilyDO>();
		if (result.isSuccess()) {
			response.setStatus(200);
			response.setVersion(1);
			response.setMsg("查询成功");
			response.setData((List<CustomerFamilyDO>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("查询失败");
		}
		return response;
	}
}
